/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-07-26 17:45:29
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.bp.repository;

import java.util.Date;
import java.util.List;

import com.joysuch.wwyt.bp.entity.BpDeviceFacilityRepairRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

/**
 * BpDeviceFacilityRepairRecordDao
 */
public interface BpDeviceFacilityRepairRecordDao extends JpaRepository<BpDeviceFacilityRepairRecord, Long>,
        QuerydslPredicateExecutor<BpDeviceFacilityRepairRecord> {
    public Page<BpDeviceFacilityRepairRecord> findAll(Specification<BpDeviceFacilityRepairRecord> spec,
                                                      Pageable pageable);

    public BpDeviceFacilityRepairRecord save(BpDeviceFacilityRepairRecord bean);

    public void delete(BpDeviceFacilityRepairRecord bean);

    @Query(value = "select * from (select * from wwyt_bp_device_facility_repair_record m where device_facility_id in (?1) order by id desc) d group by d.device_facility_id", nativeQuery = true)
    public List<BpDeviceFacilityRepairRecord> findLatestRepairRecordsBatch(List<Long> ids);

    @Query(value = "SELECT COUNT(*) FROM wwyt_bp_device_facility_repair_record WHERE DEVICE_FACILITY_ID =?1 AND ?2 BETWEEN REPAIR_START_DATE AND REPAIR_END_DATE AND delete_flag = '0'", nativeQuery = true)
    int countByDeviceFacilityIdAndNowDate(Long deviceFacilityId, Date date);
}
